//
//  CYABPhoneVC.m
//  CreditAddressBook
//
//  Created by Lee on 15/10/8.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYABPhoneVC.h"
#import "CYABPhoneCell.h"
#import "CYPerson.h"

#define rowH 50.f

@interface CYABPhoneVC ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property (nonatomic , strong) UITableView *myTableView;
@end

@implementation CYABPhoneVC

- (void)setPerson:(CYPerson *)person {
    _person = person;
}

- (CGFloat)phoneTableContentH {
    return self.myTableView.contentSize.height;
}

- (CGFloat)phoneVCHeight {
    NSInteger aa = [self.myTableView numberOfRowsInSection:0];
    NSInteger aa1 = [self.myTableView numberOfRowsInSection:1];
    return rowH * aa + rowH * aa1;
}

- (void)setScrollEnabled:(BOOL)scrollEnabled {
    if (self.myTableView) {
        self.myTableView.scrollEnabled = scrollEnabled;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置tableview
    [self setupTableView];
}

- (void)setupTableView {
    UITableView *tableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.contentInset = UIEdgeInsetsMake(0, 0, 130, 0);
//    tableview.scrollEnabled = NO;
//    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //    self.view = tableview;//使用ARSegmentPageController时，不能使用该方式，会出现莫名错误
    [self.view addSubview:tableview];
    self.myTableView = tableview;
}

#pragma mark - tableview代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.person.phones.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CYABPhoneCell *cell = [CYABPhoneCell cellWithTableView:tableView];
        cell.person = self.person;
        cell.phone = self.person.phones[indexPath.row];
        return cell;
    }
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 3;
    cell.backgroundColor = CYColor(250, 249, 255, 1);
    if (self.person.person.note) {
        label.text = self.person.person.note;
    } else {
        label.text = @"空";
        label.textColor = [UIColor grayColor];
    }
    [cell addSubview:label];
    [label autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:8.f];
    [label autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:8.f];
    [label autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:30.f];
    [label autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:16.f];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return rowH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return rowH;
    }
    return 80;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSArray *sectionTitles = @[@"手机", @"备注"];
    return sectionTitles[section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

#pragma mark - scrollview代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    CYLog(@"y:%f", scrollView.contentOffset.y);
    if (scrollView.contentOffset.y <= 0.1) {
//        self.myTableView.scrollEnabled = NO;
    }
}

#pragma mark - 重写
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.height = self.phoneVCHeight;//设置self.view.height的高度为tableview的内容的高度
}

-(NSString *)segmentTitle {
    return @"详情";
}

- (UIScrollView *)streachScrollView {
    return self.myTableView;
}

/**
 *  数据刷新
 *
 *  @param person 
 */
- (void)reloadPerson:(CYPerson *)person {
    self.person = person;
    [self.myTableView reloadData];
}
@end
